/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ucjava.chat.socialnetworking.userinterface;

import com.ucjava.chat.commserve.rmi.common.User;
import com.ucjava.chat.socialnetworking.contacts.IMyFriends;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.JList;

/**
 *
 * @author adunagan
 */
public class FriendsList extends javax.swing.JFrame {

    //private static final String HOST = "";
    //private static final int PORT = ;
    private static Registry registry;
    //create default list model
    DefaultListModel flModel = new DefaultListModel();
    
     //work around class to get strings to display properly
        class DisplayableUser extends User{
            @Override
            public String toString()
            {
                return this.getUserName();
            }
        }

    /**
     * Creates new form FriendsList
     */
    public FriendsList() throws RemoteException, NotBoundException {
        initComponents();
        

        // Pull Contacts from server and save them in an ArrayList to be used on the client
        registry = LocateRegistry.getRegistry();
        IMyFriends fr = (IMyFriends) registry.lookup("MF");
        ArrayList<User> myFriends = fr.getMyFriendsList();


        //create new FriendsStub
        //FriendsStub friends = new FriendsStub();

        //construct the ArrayList in FriendsStub
        //ArrayList<User> setUsers = friends.setUsers();


        //for each User in the ArrayList add it to the model
        for (int i = 0; i < myFriends.size(); i++) {
            //get user from array list
            User user = myFriends.get(i);
            //work around for getting proper string to display
            User dispUser = new DisplayableUser();
            dispUser.setUserName(user.getUserName());
            //add user to default list model
            flModel.add(i, dispUser);
        }

        //add model to JList
        friendsList.setModel(flModel);

        //listener for double clicks on names in list
        friendsList.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent evt) {
                JList list = (JList) evt.getSource();
                if (evt.getClickCount() == 2) {

                    //here is where we want to open a new instant messenger conversation
                    System.out.println(list.getSelectedValue());
                }
            }
        });


    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        friendsListTitle = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        friendsList = new javax.swing.JList();
        btnAddFriends = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(200, 400));

        friendsListTitle.setText("Friends");
        getContentPane().add(friendsListTitle, java.awt.BorderLayout.PAGE_START);

        jScrollPane1.setViewportView(friendsList);

        getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);

        btnAddFriends.setText("Add Friends");
        btnAddFriends.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddFriendsActionPerformed(evt);
            }
        });
        getContentPane().add(btnAddFriends, java.awt.BorderLayout.PAGE_END);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnAddFriendsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddFriendsActionPerformed
        try {
            AddFriendsUI add = new AddFriendsUI();
            add.setVisible(true);
        } catch (RemoteException ex) {
            Logger.getLogger(FriendsList.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NotBoundException ex) {
            Logger.getLogger(FriendsList.class.getName()).log(Level.SEVERE, null, ex);
        }

    }//GEN-LAST:event_btnAddFriendsActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FriendsList.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FriendsList.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FriendsList.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FriendsList.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    new FriendsList().setVisible(true);
                } catch (RemoteException ex) {
                    Logger.getLogger(FriendsList.class.getName()).log(Level.SEVERE, null, ex);
                } catch (NotBoundException ex) {
                    Logger.getLogger(FriendsList.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAddFriends;
    private javax.swing.JList friendsList;
    private javax.swing.JLabel friendsListTitle;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
}
